package cn.lystudio.rohm_time_op.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohm_time_op.entity.dto.MaintsItemDTO;
import cn.lystudio.rohm_time_op.entity.po.MaintsItemPO;
import cn.lystudio.rohm_time_op.entity.po.OpItemPO;
import cn.lystudio.rohm_time_op.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.MaintsItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/maintsItem")
@Slf4j
public class MaintsItemController {
    private MaintsItemService maintsItemService;
    @Lazy
    @Autowired
    public void setMaintsItemService(MaintsItemService maintsItemService) {
        this.maintsItemService = maintsItemService;
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page, Integer limit, @RequestParam(name = "keyword",defaultValue = "")String keyword){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<MaintsItemDTO> all = maintsItemService.getAll(keyword);
        if(CollUtil.isEmpty(all)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,all),all.size());
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer maintsItemID, Boolean enabled) {
        return SaResult.data(maintsItemService.uptMaintsItemEnabled(maintsItemID,enabled));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delMaintsItem")
    public SaResult delMaintsItem(Integer maintsItemID){
        return SaResult.data(maintsItemService.delMaintsItem(maintsItemID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addMaintsItem")
    public SaResult addMaintsItem(@RequestBody MaintsItemPO maintsItemPO){
        if(ObjectUtil.isNotEmpty(maintsItemPO.getQrTypeID()) && maintsItemPO.getQrTypeID()<1) maintsItemPO.setQrTypeID(null);
        return  SaResult.data(maintsItemService.addMaintsItem(maintsItemPO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptMaintsItem")
    public SaResult uptMaintsItem(@RequestBody MaintsItemPO maintsItemPO){
        if(ObjectUtil.isNotEmpty(maintsItemPO.getQrTypeID()) && maintsItemPO.getQrTypeID()<1) maintsItemPO.setQrTypeID(null);
        return  SaResult.data(maintsItemService.uptMaintsItem(maintsItemPO));
    }
}
